/*
 *  Copyright 2005 Blandware (http://www.blandware.com)
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.blandware.atleap.model.forum;

import com.blandware.atleap.model.core.Page;
import com.blandware.atleap.model.core.User;

import java.util.Date;

/**
 * <p>Represents a post (message) written by some one on a forum.</p>
 * <p><a href="Post.java.html"><i>View source</i></a></p>
 *
 * @author Roman Puchkovskiy <a href="mailto:roman.puchkovskiy@blandware.com">
 *         &lt;roman.puchkovskiy@blandware.com&gt;</a>
 * @version $Revision: 1.2 $ $Date: 2006/10/03 13:47:32 $
 * @struts.form include-all="false" extends="BaseForm"
 * @hibernate.joined-subclass table="`al_forum_post`" lazy="false"
 * @hibernate.joined-subclass-key column="`page_id`"
 */
public class Post extends Page {

    /**
     * Topic to which this post belongs
     */
    protected Topic topic = null;

    /**
     * Topic subject
     */
    protected String subject = null;

    /**
     * Post body
     */
    protected String body = null;

    /**
     * User object for user that created this post (null if anonymous user)
     * is authtor
     */
    protected User authorUser = null;

    /**
     * Author of post
     */
    protected String authorStr = null;

    /**
     * Email of author
     */
    protected String emailStr = null;

    /**
     * Creation date
     */
    protected Date date = new Date();

    /**
     * Whether this post is first in a topic (such post cannot be deleted apart
     * from its topic)
     */
    protected Boolean first = null;

    /**
     * Constructs an instance of Post
     */
    public Post() {
    }

    /**
     * Returns topic to which this post belongs.
     *
     * @return topic
     * @struts.form-field
	 * @hibernate.many-to-one column="`topic_id`" not-null="true"
     * outer-join="false" lazy="false"
     */
    public Topic getTopic() {
        return topic;
    }

    /**
     * Sets topic.
     *
     * @param topic topic to set
     */
    public void setTopic(Topic topic) {
        this.topic = topic;
    }

    /**
     * Returns post subject.
     *
     * @return subject
     * @struts.form-field
	 * @struts.validator type="required"
	 * @struts.validator-args arg0resource="forum.post.form.subject"
     */
    public String getSubject() {
        if (title == null || title.isEmpty()) {
            return null;
        }
        return (String) title.values().toArray()[0];
    }

    /**
     * Returns post body.
     *
     * @return body
     * @struts.form-field
     * @struts.validator type="required"
	 * @struts.validator-args arg0resource="forum.post.form.body"
     */
    public String getBody() {
        return body;
    }

    /**
     * Sets post body.
     *
     * @param body body to set
     */
    public void setBody(String body) {
        this.body = body;
    }

    /**
     * Returns post author user.
     *
     * @return author user
     * @struts.form-field
     * @hibernate.many-to-one column="`username`" not-null="false"
     * outer-join="false" lazy="false"
     */
    public User getAuthorUser() {
        return authorUser;
    }

    /**
     * Sets post author user.
     *
     * @param authorUser author user to set
     */
    public void setAuthorUser(User authorUser) {
        this.authorUser = authorUser;
    }

    /**
     * Returns a string describing author.
     *
     * @return author string
     * @struts.form-field
     * @struts.validator type="required"
	 * @struts.validator-args arg0resource="forum.post.form.authorStr"
	 * @hibernate.property
	 * @hibernate.column name="`author`" not-null="false" unique="false"
     * length="252"
     */
    public String getAuthorStr() {
        return authorStr;
    }

    /**
     * Sets a string describing author.
     *
     * @param authorStr author string to set
     */
    public void setAuthorStr(String authorStr) {
        this.authorStr = authorStr;
    }

    /**
     * Returns info about post author.
     *
     * @return author info extracted from user or authorStr
     */
    public String getAuthor() {
        if (getAuthorUser() != null) {
            return getAuthorUser().getFullName();
        } else {
            return getAuthorStr();
        }
    }

    /**
     * Returns author email.
     *
     * @return email
     * @struts.form-field
     * @struts.validator type="email"
	 * @struts.validator-args arg0resource="forum.post.form.email"
	 * @hibernate.property
	 * @hibernate.column name="`email`" not-null="false" unique="false"
     * length="252"
     */
    public String getEmailStr() {
        return emailStr;
    }

    /**
     * Sets author email.
     *
     * @param emailStr email to set
     */
    public void setEmailStr(String emailStr) {
        this.emailStr = emailStr;
    }

    /**
     * Returns author email, whether it's specified directly or through user.
     *
     * @return email
     */
    public String getEmail() {
        if (getAuthorUser() != null) {
            return getAuthorUser().getEmail();
        } else {
            return getEmailStr();
        }
    }

    /**
     * Returns creation date.
     *
     * @return creation date
     * @hibernate.property
	 * @hibernate.column name="`date`" not-null="true" unique="false"
     */
    public Date getDate() {
        return date;
    }

    /**
     * Sets creation date.
     *
     * @param date date to set
     */
    public void setDate(Date date) {
        this.date = date;
    }

    /**
     * Returns whether this is first post in topic.
     *
     * @return whether this is first
     * @struts.form-field
	 * @hibernate.property column="`first`" not-null="true" type="true_false"
     */
    public Boolean getFirst() {
        return first;
    }

    /**
     * Sets whether this is first post in topic.
     *
     * @param first whether this is first
     */
    public void setFirst(Boolean first) {
        this.first = first;
    }

    
}
